using System;
using System.Windows;
using System.Windows.Threading;

namespace WaistGuard
{
    public partial class PreRestWindow : Window
    {
        private DispatcherTimer? timer;
        private int remainingSeconds = 10;
        public bool IsDelayed { get; private set; }
        private bool isManualTriggered;

        public PreRestWindow(bool isManual = false)
        {
            InitializeComponent();
            isManualTriggered = isManual;
            remainingSeconds = isManual ? 5 : 10;
            
            timer = new DispatcherTimer
            {
                Interval = TimeSpan.FromSeconds(1)
            };
            timer.Tick += Timer_Tick;
            timer.Start();
            UpdateCountdown();
        }

        private void Timer_Tick(object? sender, EventArgs e)
        {
            remainingSeconds--;
            UpdateCountdown();
            
            if (remainingSeconds <= 0)
            {
                timer?.Stop();
                this.Close();
            }
        }

        private void UpdateCountdown()
        {
            var minutes = remainingSeconds / 60;
            var seconds = remainingSeconds % 60;
            CountdownText.Text = isManualTriggered ? 
                $"即将开始休息：{minutes}:{seconds:D2}" :
                $"工作时间到了，{minutes}:{seconds:D2}后开始休息";
        }

        private void DelayButton_Click(object sender, RoutedEventArgs e)
        {
            IsDelayed = true;
            timer?.Stop();
            this.Close();
        }

        private void DragWindow(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (e.LeftButton == System.Windows.Input.MouseButtonState.Pressed)
            {
                DragMove();
            }
        }

        private void CloseWindow(object sender, RoutedEventArgs e)
        {
            IsDelayed = true;
            timer?.Stop();
            this.Close();
        }
    }
} 